<template>
	<div>
    <!-- <div class="container"> -->
		<div class="handle-box">
			<label class="label" >资源名称：</label>
			<el-input class="handle-input" v-model="options.res_name"></el-input>
			<label class="label" >资源类型：</label>
			<el-select
			  v-model="options.res_type" 
				filterable
				allow-create
				style="width: 130px;"
			   >
				<el-option
					v-for="item in options.resources"
					:key="item.value"
					:label="item.label"
					:value="item.value"
					/>
			</el-select>
			<el-button color="#0ab394" class="handle-btn"  @click="handleSearch">查询</el-button>
			<el-row class="top-btn">
                <el-button color="#0ab394"  @click="openAdd">增加</el-button>
                <el-button color="#e35148" @click="handleAllDelete">删除</el-button>                              
			</el-row>
			</div>
			<el-table ref="multipleTableRef" 
				:header-cell-style="{color:'#2A62BC'}"
			 	:data="tableData"   
			 	style="width: 100%;" 
                size="small"
                border
				@selection-change="handleSelectionChange">
                <el-table-column   type="selection" width="35" />
				<el-table-column  prop="res_name" label="资源名称"  ></el-table-column>
				<el-table-column prop="res_type" label="资源类型"></el-table-column>
                <el-table-column prop="res_size" label="资源大小"></el-table-column>
                <el-table-column  label="操作">
					<template #default="scope">
							<el-row class="right-btn">
						    <!-- <el-button color="#0ab394"  @click="downloadRes(scope.$index,scope.row)" >
								<i class="iconfont icon-down2 white"></i></el-button> -->
				            <el-button color="#e35148"   @click="handleDelete(scope.$index)" >
								<i class="iconfont icon-xiazai14 white"></i></el-button>
						</el-row>
					</template>
				
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div class="pagination">
				<el-pagination
				    :current-page="query.pageIndex"
					:page-size="query.pageSize"
					:page-sizes="[5, 10, 15, 20]"
					size="small"
					layout="total, sizes, prev, pager, next, jumper"
					:total="pageTotal"
					@size-change="handleSizeChange"
					@current-change="handlePageChange"/>
			</div>
	<!-- </div> -->

		<!-- 新增的弹窗 -->
		<el-dialog title="增加" v-model="addVisible" width="675" >
			<!-- 添加组件 -->
			<fileAdd></fileAdd>
			<template #footer>
				<span class="dialog-footer">
					<el-button color="#0ab394" @click="addVisible = false">关闭</el-button>
					<el-button color="#0ab394" @click="addVisible = false">确 定</el-button>
				</span>
			</template>
		</el-dialog>

		<!-- </div> -->
	</div>
</template>

<script setup >
import { ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Delete, Edit, Search, Plus } from '@element-plus/icons-vue';
import { getRes } from '../../api/clazz' ;
import fileAdd from './fileAdd.vue';
const options = reactive({
	res_name:'',
	res_type:'',
	res_size:'',
	id:'',
	resources:[
		{
			value: '全部资源',
			label: '全部资源',
		},
	{
		value: '公共资源',
		label: '公共资源',
	},
	{
		value: '课程资源',
		label: '课程资源',
	},
	],
})

//分页字体
const small = ref(true)
// const size = ref<'default' | 'large' | 'small'>('default')

//分页数据
const query = reactive({
	pageIndex: 1,
	pageSize: 5,
});
//

//表格数据
const tableData = ref();
//总页数
const pageTotal = ref(0);
const multipleTableRef = ref()
const state = reactive({
	multipleSelection:[]
})

// 获取表格数据  分页是数据截取  后台结合一起后  直接返回分页的后的数据
const getData = () => {
    getRes({pageCurrent:query.pageIndex,pageSize:query.pageSize}).then(res=>{
        console.log(res,"---res---")
        // tableData.value = res.data.
		let start  = (query.pageIndex-1)*query.pageSize;//开始位置
        let end =query.pageIndex*query.pageSize  ;//结束位置 数组的截取的时候 [start,end) suo所以不用-1
        tableData.value = res.data.slice(start,end);
        // tableData.value = res.data;
        pageTotal.value = res.data.length
    })
	
};
getData();
//多选框权限
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}
// 分页导航
const handleSizeChange = (val) => {
  // console.log(`${val} items per page`)
  query.pageSize = val;
  getData()
}
const handlePageChange = (val) => {
	query.pageIndex = val;
	console.log(val,"==")
	console.log(query.pageSize)
	getData()
};

// 删除单个操作
const handleDelete = (index) => {
	// 二次确认删除
	ElMessageBox.confirm('确定要删除吗？', '提示', {
		type: 'warning'
	})
	.then(() => {
		ElMessage.success('删除成功');
		//to 传递id 到后台 
		tableData.value.splice(index, 1);
		// 删除成功后
		// getData()
	})
	.catch(() => {});
};

//删除多个数据  to......
const handleAllDelete =()=>{
	// console.log('删除多个')
	console.log(state.multipleSelection,'===拿到所有选择的数据====')
	if (state.multipleSelection.length==0) {
		ElMessage.error('请选择项')
		return
  	}
	// console.log(state.multipleSelection.map(item=>item.id))
	//axios删除的操作
	// deleteAllAdmin({ids:state.multipleSelection.map(item=>item.id)})	
}

// 查询操作  to... 需要自己实现与后台交互
const handleSearch = () => {
	query.pageIndex = 1;
	getData();
};

//下载
const downloadRes=(index,row)=>{
	ElMessageBox.alert('下载成功', '127.0.0.1:5173 显示', {
    // if you want to disable its autofocus
    // autofocus: false,
    confirmButtonText: 'OK',
    // callback: (action) => {
    //   ElMessage({
    //     type: 'info',
    //     message: `action: ${action}`,
    //   })
    // },
  })
}

//
const addVisible = ref(false)
const openAdd =()=>{
	addVisible.value=true
}

</script >
<style lang="less" scoped>
.handle-box {
	margin-bottom: 20px;   
	.handle-btn{
		margin-left: 8px;
	}
}

.handle-input {
	width: 130px;
    margin-right: 8px;
}
.label{
   display: inline-block;
   max-width:100%;
   margin: 2px 2px;
   font-weight: 700; 
}
.table {
	width: 100%;
	font-size: 13px;
}
.red {
	color: #F56C6C;
}
.blue {
	color: #1296e2;
	font-weight: bold;
}
.mr10 {
	margin-right: 10px;
}
.table-td-thumb {
	display: block;
	margin: auto;
	width: 40px;
	height: 40px;
}
.cell{
    display: inline-block;
    .el-button{
        margin-left: 0px;
    }
}
.el-col {
  border-radius: 4px;
  margin-left: 10px;
}
.el-button-group{
    float: right;
}
.top-btn{
	float: right;
	.el-button{
		margin-left: 5px;
	}
}
.right-btn{
 	justify-content:center;
	.el-button{
		height: 22px;
		// width: 60px;
		margin-right: 10px;
		padding: 1px 5px;
	}
}


</style>